//
//  HLHomeController.m
//  weibo-OC
//
//  Created by 黄力 on 15/12/15.
//  Copyright © 2015年 HLTest. All rights reserved.
//

#import "HLHomeController.h"
#import "HLUserAccountViewModel.h"
#import <AFNetworking.h>
#import "HLStatuses.h"
#import "HLHomeCell.h"
#import "HLStatusesViewModel.h"
#import "HLHomeRefreshControl.h"

@interface HLHomeController ()

//存放模型数组的属性
@property (nonatomic,strong) NSArray *statusesArr;

//底部加载更多指示器
@property (nonatomic,strong) UIActivityIndicatorView *footerIndicatorView;
//顶部加载更多指示器
@property (nonatomic,strong) HLHomeRefreshControl *headerRefreshControl;
@end

@implementation HLHomeController

//自定义重用标示
static NSString *homeCellID = @"HomeCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //判断用户是否登录
    if ([HLUserAccountViewModel getUserLogin]) {
        [self setupUI];
    }else {
       [self.visitorView setVisitorTitle:@"首页" withLagerImageName:nil];
    }
    
}

//设置界面
- (void)setupUI {
    //注册cell
    [self.tableView registerClass:[HLHomeCell class] forCellReuseIdentifier:homeCellID];
    //设置预估行高
    self.tableView.estimatedRowHeight = 300;
    //设置根据内容计算行高
    self.tableView.autoresizesSubviews = YES;
    //设置分割线样式
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //添加底部视图
    self.tableView.tableFooterView = self.footerIndicatorView;
    //添加顶部记载视图
    [self.tableView addSubview:self.headerRefreshControl];
    //添加响应事件
    [self.headerRefreshControl addTarget:self action:@selector(loadHomeData) forControlEvents:UIControlEventValueChanged];
    //加载数据
    [self loadHomeData];
}

//加载数据
-(void)loadHomeData {
    //定义两个变量
    NSString *since_id;
    NSString *max_id;
    //当底部指示器正在转动的时候加载更多
    if (self.footerIndicatorView.isAnimating) {
        //获取最后一个数据
        HLStatuses *lastStatus = self.statusesArr.lastObject;
        max_id = lastStatus.statusId;
    }else if ([self.headerRefreshControl isRefreshing]) {
        //获取第一个数据
        HLStatuses *firstStatus = self.statusesArr.firstObject;
        since_id = firstStatus.statusId;
    }
    [HLStatusesViewModel loadHomeStatusesDataWithSinceId:since_id withMaxId:max_id withFinshedBlock:^(NSArray *statusesArr) {
        //创建可变数组
        NSMutableArray *arrM = [NSMutableArray array];
        //判断
        if (max_id != nil) {
            //拼接数据
            [arrM addObjectsFromArray:self.statusesArr];
            [arrM addObjectsFromArray:statusesArr];
            //结束底部动画
            [self.footerIndicatorView stopAnimating];
        }else if (since_id != nil) {
            //拼接数据
            [arrM addObjectsFromArray:statusesArr];
            [arrM addObjectsFromArray:self.statusesArr];
            //结束刷新
            [self.headerRefreshControl endRefreshing];
        }else {
            arrM = [NSMutableArray arrayWithArray:statusesArr];
        }
        self.statusesArr = arrM;
        //刷新数据
        [self.tableView reloadData];
    }];
 
    
}

#pragma mark - 数据源方法
//设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.statusesArr.count;
}

//设置每行内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //当显示的时最后一条数据时底部指示器开始转动,并加载更多数据
    if ( !self.footerIndicatorView.isAnimating && indexPath.row == self.statusesArr.count - 1) {
        [self.footerIndicatorView startAnimating];
        [self loadHomeData];
    }
    
    //获取cell
    HLHomeCell *cell = [tableView dequeueReusableCellWithIdentifier:homeCellID forIndexPath:indexPath];
    //获取模型数据
    HLStatuses *statuses = self.statusesArr[indexPath.row];
    //设置数据
    cell.statuses = statuses;
    //返回
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//懒加载
- (UIActivityIndicatorView *)footerIndicatorView {
    if (_footerIndicatorView == nil) {
        _footerIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    }
    return _footerIndicatorView;
}

- (HLHomeRefreshControl *)headerRefreshControl {
    if (_headerRefreshControl == nil) {
        _headerRefreshControl = [[HLHomeRefreshControl alloc] init];
    }
    return _headerRefreshControl;
}


@end
